package com.maimang.platform.controller.system;


import com.maimang.platform.annotation.Log;
import com.maimang.platform.base.BaseController;
import com.maimang.platform.base.Result;
import com.maimang.platform.common.entity.SysNotice;
import com.maimang.platform.enums.BusinessType;
import com.maimang.platform.page.TableDataInfo;
import com.maimang.platform.common.service.SysNoticeService;
import com.maimang.platform.common.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @类名 SysNoticeController
 * @描述 <br>公告信息操作处理</br>
 * @日期 2019-03-28
 * @email jintaozhao@qq.com
 */
@Controller
@RequestMapping("/system/notice")
@Api(value = "公告信息", description = "系统公告信息操作处理")
public class SysNoticeController extends BaseController {
	private String prefix = "system/notice";

	@Autowired
	private SysNoticeService noticeService;

	@RequiresPermissions("system:notice:view")
	@GetMapping()
	public String notice() {
		return prefix + "/notice";
	}

	/**
	 * 查询公告列表
	 */
	@RequiresPermissions("system:notice:list")
	@ApiOperation(value = "公告列表", notes = "查询公告信息列表并返回前端表格数据")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(SysNotice notice) {
		startPage();
		List<SysNotice> list = noticeService.selectNoticeList(notice);
		return getDataTable(list);
	}

	/**
	 * 新增公告
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存公告
	 */
	@RequiresPermissions("system:notice:add")
	@ApiOperation(value = "新增公告", notes = "新增保存公告信息")
	@Log(title = "通知公告", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(SysNotice notice) {
		notice.setCreateUser(ShiroUtils.getLoginName());
		return toResult(noticeService.insertNotice(notice));
	}

	/**
	 * 修改公告
	 */
	@GetMapping("/edit/{noticeId}")
	public String edit(@PathVariable("noticeId") Long noticeId, ModelMap modelMap) {
		modelMap.put("notice", noticeService.selectNoticeById(noticeId));
		return prefix + "/edit";
	}

	/**
	 * 修改保存公告
	 */
	@RequiresPermissions("system:notice:edit")
	@ApiOperation(value = "修改公告", notes = "修改并保存公告信息")
	@Log(title = "通知公告", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(SysNotice notice) {
		notice.setUpdateUser(ShiroUtils.getLoginName());
		return toResult(noticeService.updateNotice(notice));
	}

	/**
	 * 删除公告
	 */
	@RequiresPermissions("system:notice:remove")
	@ApiOperation(value = "删除公告", notes = "删除公告信息并返回操作结果")
	@Log(title = "通知公告", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public Result remove(String ids) {
		return toResult(noticeService.deleteNoticeByIds(ids));
	}
}
